import asyncio
import random

from fastapi import Request
from fastapi import Depends
from fastapi_jwt_auth import AuthJWT
from fastapi.responses import JSONResponse

from new_auth.schemas.auth import auth
from common.base.base_routers import get_custom_router
from services.new_services.technic_page.api import TechnicPageApi
from services.new_services.technic_page.schemas.block_type import BlockTypeRequest
from services.new_services.technic_page.schemas.technic_configuration import TechnicConfiguration
from services.automatic_control.api import AutomaticControlApi
from services.new_services.new_server_integration.schemas.zcalibration import ZCalibrationTelemetry, ZCalibrationInfo,\
    ZCalibrationStatus, ZCalibrationDevice
from services.server_integration.api import ServerIntegrationApi


technic_api = TechnicPageApi()
si_api = ServerIntegrationApi()
ac_api = AutomaticControlApi()

technic_router = get_custom_router(
    technic_api, prefix='/technic_page', tags=['technic_page'], base_routers=False
)


@technic_router.post('/get-cviya-list-by-block-type', response_class=JSONResponse)
async def get_cviya_list_by_block_type(block_type: BlockTypeRequest, request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await technic_api.get_cviya_list(block_type)


@technic_router.get('/clear-alarms', response_class=JSONResponse)
async def clear_alarms(request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await technic_api.clear_alarms()


@technic_router.get('/get-technic-page-information', response_class=JSONResponse)
async def get_technic_page_information(request: Request, authorize: AuthJWT = Depends()):
    await auth(authorize, request)
    return await technic_api.get_technic_page_information()


@technic_router.post('/set-unit-config-by-technic', response_class=JSONResponse)
async def set_unit_config_by_technic(config: TechnicConfiguration, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    return await technic_api.set_unit_config_by_technic(config, user_role)


@technic_router.post('/zcalibration-calibrate', response_class=JSONResponse)
async def zcalibrate(device: ZCalibrationDevice, target_power: float, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role not in ['adjuster', 'server']:
        return 'expected adjuste'

    return await si_api.zcalibration_calibrate(device.get_index(), target_power)


@technic_router.get('/zcalibration-telemetry', response_class=JSONResponse)
async def ztelemetry(device: ZCalibrationDevice, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role not in ['adjuster', 'server']:
        return 'expected adjuste'

    return await si_api.zcalibration_telemetry(device.get_index())


@technic_router.get('/zcalibration-info', response_class=JSONResponse)
async def zinfo(device: ZCalibrationDevice, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role not in ['adjuster', 'server']:
        return 'expected adjuster'

    return await si_api.zcalibration_info(device.get_index())


@technic_router.post('/zcalibration-zsave', response_class=JSONResponse)
async def zsave(device: ZCalibrationDevice, info: ZCalibrationInfo, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role not in ['adjuster', 'server']:
        return 'expected adjuster'

    return await si_api.zcalibration_save(device.get_index(), info)


@technic_router.get('/set-rtr-normal-vswr', response_class=JSONResponse)
async def set_rtr_normal_vswr(index: int, vswr: float, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role not in ['adjuster', 'server']:
        return 'expected adjuster'

    return await si_api.set_rtr_normal_vswr(index, vswr)


@technic_router.get('/get-rtr-normal-vswr', response_class=JSONResponse)
async def get_rtr_normal_vswr(index: int, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role not in ['adjuster', 'server']:
        return 'expected adjuster'

    return await si_api.get_rtr_normal_vswr(index)


@technic_router.get('/get-all-rtr-normal-vswr', response_class=JSONResponse)
async def get_all_rtr_normal_vswr(request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role not in ['adjuster', 'server']:
        return 'expected adjuster'

    return [await si_api.get_rtr_normal_vswr(0), await si_api.get_rtr_normal_vswr(1)]


@technic_router.get('/calibrate-rtr-vswr', response_class=JSONResponse)
async def calibrate_rtr_vswr(index: int, request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role not in ['adjuster', 'server']:
        return 'expected adjuster'

    return await si_api.calibrate_rtr_vswr(index)


@technic_router.get('/enable-cp', response_class=JSONResponse)
async def enable_cp(request: Request, server_user_role: str = None, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role == 'server' and server_user_role == 'adjuster':
        user_role = server_user_role

    return await technic_api.enable_cp(user_role)


@technic_router.get('/get-cp-status', response_class=JSONResponse)
async def get_cp_status(request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)

    return await technic_api.get_cp_status()


@technic_router.get('/digital-tune-all', response_class=JSONResponse)
async def digital_tune_all(request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role != 'adjuster':
        return 'expected adjuster'

    return await si_api.digital_tune_all()


@technic_router.get('/get-ad-ul-dl', response_class=JSONResponse)
async def get_ad_ul_dl(request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role != 'adjuster':
        return 'expected adjuster'

    return await si_api.get_ad_ul_dl()

@technic_router.get('/get-actual-reference-freq-40mhz', response_class=JSONResponse)
async def get_actual_reference_freq_40mhz(request: Request, authorize: AuthJWT = Depends()):
    is_user, user_role = await auth(authorize, request)
    if user_role != 'adjuster':
        return 'expected adjuster'

    return await ac_api.get_actual_reference_freq_40mhz()